package com.ccys.cms.controller.productAndService;


import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.productAndService.ProductListDetailSerivce;
import com.ccys.cms.service.productAndService.ProductListService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.dto.web.productAndService.ProductListDto;
import com.ccys.db.entity.TblProductList;
import com.ccys.db.entity.TblProductListDetail;
import com.ccys.db.vo.web.productAndService.ProductListVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/productAndService")
@Api(tags = "产品集合管理")
public class ProductListController {

    @Resource
    private ProductListService service;

    @Resource
    private ProductListDetailSerivce detailSerivce;


    @PostMapping("productList")
    @ApiOperation("新增")
    @Transactional
    public ResponseResult insert(TblProductList model) {

        int i = service.insertOrUpdate(model);
        return ResponseResult.update(i);
    }

    @PutMapping("productList")
    @ApiOperation("修改")
    @Transactional
    public ResponseResult update(TblProductList model) {

        int i = service.insertOrUpdate(model);
        return ResponseResult.update(i);
    }


    @PostMapping("productList/clone")
    @ApiOperation("克隆")
    @Transactional
    public ResponseResult clone(String id,String name){
        TblProductList entityById = service.findEntityById(id);
        entityById.setId(null);
        entityById.setName(name);
        int i = service.insertOrUpdate(entityById);
        String listId = entityById.getId();
        List<TblProductListDetail> detailList = detailSerivce.findListByLimit(TblProductListDetail.class, new ArrayList<String>() {{
            add("del_flag = 0");
            add(" list_id = '" + id + "'");
        }}, "created_on desc");
        detailList.forEach(x->{
            x.setId(IdUtil.getSnowflakeNextIdStr());
            x.setListId(listId);
        });
        detailSerivce.insertAll(detailList);
        return ResponseResult.update(i);
    }





    @DeleteMapping("productList")
    @ApiOperation("根据Id删除")
    @Transactional
    public ResponseResult delete(String id) {

        TblProductList model = new TblProductList();
        model.setId(id);
        model.setDelFlag(NumEnum.DEL.getCode());
        int i = service.updateEntityById(model);
        //删除listDetail
        detailSerivce.deleteDetails(id);
        return ResponseResult.update(i);
    }


    @GetMapping("productList")
    @ApiOperation(value = "查询分页", notes = "排序字段 t1.name / t1.status /t2. user_fullname / t1.created_on")
    public ResponseResult<PageInfo<ProductListVo>, Object> getPage(ProductListDto dto) {

        return service.getPage(dto);
    }


}
